package com.scorpio.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.scorpio.common.utils.DateUtils;
import com.scorpio.common.utils.StringUtils;
import com.scorpio.order.domain.OrderEntity;
import com.scorpio.order.domain.OrderWaybillEntity;
import com.scorpio.order.enums.OrderStatusEmun;
import com.scorpio.order.service.IOrderService;
import com.scorpio.order.service.IOrderWaybillService;
import com.scorpio.redis.cachelock.RedisLock;
import com.scorpio.sdk.dto.SFRouteRespsModel;
import com.scorpio.sdk.service.SFService;
import com.scorpio.sdk.service.WXPayService;
import com.scorpio.sdk.service.WeiXinService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("scorpioTask")
public class ScorpioTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private RedisLock redisLock;

    @Autowired
    private IOrderService orderService;


    @Autowired
    private IOrderWaybillService orderWaybillService;

    @Autowired
    private SFService sfService;

    @Autowired
    private WXPayService wxPayService;

    //扫描业务任务
    public void scanBusiness() {
        String key = "ScorpioTask_scanBusiness";
        if (redisLock.lock(key, 60)) {

            //扫描过期订单
            try {
                int count = orderService.scanOverdueb();
                logger.info("失效订单 : " + count + "条");
            } catch (Exception e) {
                logger.error("订单更新失败", e);
            }


            //扫描订单物流信息保存
            try {
                List<OrderEntity> list = orderService.selectByStatus(OrderStatusEmun.Status3);
                int count = 0;
                for (OrderEntity entity : list) {
                    try {
                        if (!StringUtils.isEmpty(entity.getLogisticsNumber())) {
                            List<SFRouteRespsModel> respsModels = sfService.searchRoutes(entity.getLogisticsNumber());
                            OrderWaybillEntity waybillEntity = new OrderWaybillEntity();
                            waybillEntity.setOrderOn(entity.getOrderSn());
                            waybillEntity.setContent(JSONArray.toJSONString(respsModels));
                            orderWaybillService.save(waybillEntity);
                            for (SFRouteRespsModel respsModel : respsModels) {
                                if ("50".equals(respsModel.getOpcode())) {
                                    orderService.updateFinish(entity);
                                    count++;
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                logger.info("更新订单信息 : " + count + "条");
            } catch (Exception e) {
                logger.error("订单更新失败", e);
            }

            //扫描超时订单
            try {
                List<OrderEntity> list = orderService.timeoutOrders();
                int count = 0;
                for (OrderEntity entity : list) {
                    if (entity.getPayTime() != null && System.currentTimeMillis() - entity.getPayTime().getTime() >= DateUtils.MILLIS_PER_DAY * 15) {
                        orderService.updateFinish(entity);
                        wxPayService.payClose(entity.getOrderSn());
                        count++;
                    }
                }
                logger.info("更新订单信息 : " + count + "条");
            } catch (Exception e) {
                logger.error("订单更新失败", e);
            }
        }
    }
}
